#include "../include/DbResult.h"

namespace ZY {

    DbResult::DbResult( MYSQL_RES * res ) : _res( res )
    {
        _max_field_num = mysql_num_fields( res );
        MYSQL_FIELD *f;
        _fieldName.clear();
        for( int i = 0 ; i < _max_field_num ; i ++ ) {
            f = mysql_fetch_field_direct( res , i);
            string str( f->name , strlen( f->name ) );
            _fieldName.push_back( str );
        }
    }

    DbResult::~DbResult()
    {
        //dtor
        if ( _res ) {
            mysql_free_result( _res );
        }
    }

    bool DbResult::fetchRow( DbResult::FIELD_MAP & _row )
    {
        MYSQL_ROW row;

        _row.clear();

        row = mysql_fetch_row( _res );



        if ( ! row ) {
            return false;
        }

        for( int i = 0 ; i < _max_field_num ; i ++ ) {
            _row[ _fieldName[i] ] = row[i];
        }
        return true;
    }

    bool DbResult::fetchRowArray( DbResult::FIELD_ARRAY &  _rowArr )
    {
        MYSQL_ROW row;

        _rowArr.clear();

        row = mysql_fetch_row( _res );

        if ( ! row ) {
            return false;
        }

        for( int i = 0 ; i < _max_field_num ; i ++ ) {
            _rowArr.push_back( row[i] );
        }
        return true;
    }
}
